<template>
  <div v-if="showButtons && !hideOnLogin">
    <el-button
      class="top-btn"
      type="text"
      icon="el-icon-upload2"
      @click="scrollToTop"
      :class="{ hidden: !showTopBtn }"
    />
    <el-button
      class="button-btn"
      type="text"
      icon="el-icon-download"
      @click="scrollToBottom"
      :class="{ hidden: !showBottomBtn }"
    />
  </div>
</template>

<script>
export default {
  name: "TopBottomButton",
  data() {
    return {
      showTopBtn: false,
      showBottomBtn: false,
    };
  },
  props: {
    // 是否显示按钮
    showButtons: {
      type: Boolean,
      default: true,
    },
    // 是否在登录页面隐藏按钮
    hideOnLogin: {
      type: Boolean,
      default: false,
    },
    // 滚动到顶部的阈值
    topThreshold: {
      type: Number,
      default: 20,
    },
    // 滚动到底部的阈值
    bottomThreshold: {
      type: Number,
      default: 20,
    },
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: "smooth", // 平滑滚动效果
      });
    },
    scrollToBottom() {
      window.scrollTo({
        top: document.body.scrollHeight,
        behavior: "smooth", // 平滑滚动效果
      });
    },
    handleScroll() {
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
      this.showTopBtn = scrollTop > this.topThreshold; // 当滚动超过阈值时显示按钮
      this.showBottomBtn = scrollTop > this.bottomThreshold; // 当滚动超过阈值时显示按钮
    },
  },
};
</script>

<style scoped>
/* 隐藏按钮的样式 */
.hidden {
  opacity: 0;
  transition: opacity 0.3s ease;
}

/* 顶部按钮样式 */
.top-btn {
  position: fixed;
  bottom: 66px;
  right: 20px;
  background: linear-gradient(135deg, #ff7eb3, #ff57a3);
  color: white;
  border: none;
  border-radius: 50%;
  width: 35px;
  height: 35px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 5px 15px rgba(255, 57, 163, 0.4);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  z-index: 999;
}

.top-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 10px 20px rgba(255, 57, 163, 0.6);
}

/* 底部按钮样式 */
.button-btn {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: linear-gradient(135deg, #ff7eb3, #ff57a3);
  color: white;
  border: none;
  border-radius: 50%;
  width: 35px;
  height: 35px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 5px 15px rgba(255, 57, 163, 0.4);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  z-index: 999;
}

.button-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 10px 20px rgba(255, 57, 163, 0.6);
}
</style>
